import socket

# 创建UDP socket对象
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 绑定IP地址和端口号
server_socket.bind(('localhost', 8000))

while True:
    # 接收数据
    data, addr = server_socket.recvfrom(1024)
    print('Received message: %s from %s' % (data.decode(), addr))

    # 发送数据
    replyData = 'Hello, %s!' % addr[0]
    print(replyData)
    server_socket.sendto(replyData.encode(encoding='utf-8'), addr)
